package MediaOnline.GUI;

import MediaOnline.Adapter.DanhSachTapPhimAdapter;
import MediaOnline.Data.ActorData;
import MediaOnline.Data.ListFilmForMedia;
import MediaOnline.Data.ThongTinDienVien;
import MediaOnline.Data.filmDetail;
import MediaOnline.Data.userData;
import MediaOnline.Data.videoData;
import MediaOnline.General.DownloadImages;
import MediaOnline.General.checkNetwork;
import MediaOnline.General.paramConst;
import MediaOnline.WebService.SessionRegister;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.text.Html;
import android.text.Layout;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class actFilmDetailInfo2 extends Activity {
	
	public static ImageView imgFilmFullImg;
	public static TextView txtFilmNameChinh;
	public static TextView txtFilmNamePhu;
	public static TextView txtCountry;
	public static TextView txtLanguage;
	public static TextView txtDuration;
	public static TextView txtDescription;
	public static TextView txtSound;
	public static TextView txtRate;
	public static TextView txtPrice;
	public static RatingBar rbDanhGiaPhim;
	public static Button btnPlay;
	public static Button btnBack;
	public static Button btnBaoLoi;
	public static RatingBar Rating;
	public static TableLayout tblLayout;
	public static TableLayout tblActors;
	public static GridView gDanhSachTapPhim;
	public static Context ctx;
	public static int ilink=0;
	public static int idx=0;
	public static ProgressDialog LoadPhim; 
	public static DanhSachTapPhimAdapter dspAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filminfo2);
		//giao dien
		imgFilmFullImg = (ImageView) findViewById(R.id.filmthumb2);
		txtFilmNameChinh = (TextView) findViewById(R.id.txtTenChinh);
		txtFilmNamePhu = (TextView) findViewById(R.id.txtTenPhu);
		txtCountry = (TextView) findViewById(R.id.txtquocgia2);
		txtLanguage = (TextView) findViewById(R.id.txtngonngu2);
		txtDuration = (TextView) findViewById(R.id.txtthoiluong);
		txtDescription = (TextView) findViewById(R.id.txtmota2);
		txtSound = (TextView) findViewById(R.id.txtamthanh2);
		txtPrice = (TextView) findViewById(R.id.txtGia2);
		btnPlay = (Button) findViewById(R.id.btnDetail2XemPhim);
		btnBack = (Button) findViewById(R.id.btnDetail2Back);
		btnBaoLoi = (Button) findViewById(R.id.btnDetail2BaoLoi);
		rbDanhGiaPhim = (RatingBar) findViewById(R.id.rbDanhGiaPhim);
		rbDanhGiaPhim.setEnabled(false);
		tblActors = (TableLayout) findViewById(R.id.tblActors);
		gDanhSachTapPhim = (GridView) findViewById(R.id.gDanhSachTapPhim);
		ctx = actFilmDetailInfo2.this;
		dspAdapter = new DanhSachTapPhimAdapter(ctx);
		//Font cho film name chinh
		Typeface tf = Typeface.createFromAsset(getAssets(),"font/fontTenPhimChinh.TTF");
		txtFilmNameChinh.setTypeface(tf);
		//Kiem tra do phan giai man hinh
		Display display = getWindowManager().getDefaultDisplay();
		int width = (int) (display.getWidth()*0.65);
		int Cot = width/100;
		gDanhSachTapPhim.setNumColumns(Cot);
		
		//Bắt sự kiện chọn tập phim
		gDanhSachTapPhim.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int i,
					long arg3) {
				// TODO Auto-generated method stub
				ListFilmForMedia.Tap = i;
				//videoData.MAIN_KEY = videoData.KEY2;
				final String link = ListFilmForMedia.lstFilmUrl.get(i);
				SessionRegister s = new SessionRegister(ctx, link);
				s.reg();
			}
		});
		
		
		
		btnBack.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		btnPlay.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Kiểm tra nếu dùng các kết nối chậm thì không cho xem phim
			/*	checkNetwork chk  = new checkNetwork();
				if (chk.UseMobileConnection(ctx)) {
					//============Hien thi thong bao=======================
					AlertDialog alertDialog = new AlertDialog.Builder(actFilmDetailInfo2.this).create();
					alertDialog.setTitle("Thông báo!");
					alertDialog.setMessage("Để đảm bảo xem phim với chất lượng tốt nhất. Chúng tôi chỉ đáp ứng yêu cầu xem phim khi sử dụng mạng có dây hoặc wifi. Mong quý khách thông cảm!");
					alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
					   public void onClick(DialogInterface dialog, int which) {

					   }
					});
					alertDialog.show();
					//============================================================================
				}*/
				/*else{*/
					
					SessionRegister s = new SessionRegister(ctx, filmDetail.Url);
					s.reg();
					
					
					
					
					dialogShow(ctx);
					
			//	}
			}
		});
		
		
		//Xu li button khi co su kien focus
		btnBack.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					btnBack.setBackgroundResource(R.drawable.filmdetail2btnbackhov);
				}
				else{
					btnBack.setBackgroundResource(R.drawable.filmdetail2btnback);
				}
			}
		});
		
		btnPlay.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					btnPlay.setBackgroundResource(R.drawable.filmdetail2btnplayhov);
				}
				else{
					btnPlay.setBackgroundResource(R.drawable.filmdetail2btnplay);
				}
			}
		});
		
		btnBaoLoi.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					btnBaoLoi.setBackgroundResource(R.drawable.filmdetail2btnbaoloihov);
				}
				else{
					btnBaoLoi.setBackgroundResource(R.drawable.filmdetail2btnbaoloi);
				}
			}
		});
		
		
	}
	
	
	public static void UpdateInfo(){
		//Gan du lieu
		txtFilmNameChinh.setText(filmDetail.FilmNameChinh);
		txtFilmNamePhu.setText(filmDetail.FilmNamePhu);
		txtCountry.setText(Html.fromHtml("<b>"+txtCountry.getText()+"</b>"+" "+filmDetail.Country));
		txtLanguage.setText(Html.fromHtml("<b>"+txtLanguage.getText()+"</b>"+" "+filmDetail.Language));
		txtDuration.setText(Html.fromHtml("<b>"+txtDuration.getText()+"</b>"+" "+filmDetail.Duration));
		txtDescription.setText(Html.fromHtml("<b>"+txtDescription.getText()+"</b>" +filmDetail.Description));
		txtSound.setText(Html.fromHtml("<b>"+txtSound.getText()+"</b>"+" "+filmDetail.Sound));
		//txtRate.setText(txtRate.getText()+": "+filmDetail.Rate+"/5");
		if (Integer.parseInt(filmDetail.Price)==0) {
			txtPrice.setText("Miễn phí");
		}
		else {
			txtPrice.setText(filmDetail.Price+"đ");
		}
		
		//Neu co hinh thi load hinh, khong thi lay hinh mac dinh
		if (filmDetail.FilmImgUrl!=null){
			imgFilmFullImg.setImageDrawable(filmDetail.FilmImgUrl);
		}
		else{
			imgFilmFullImg.setImageResource(R.drawable.movienoimg);
		}
		rbDanhGiaPhim.setRating(Float.parseFloat(filmDetail.Rate));
		
		Log.d("So tap::::::::", ""+ListFilmForMedia.lstTap.size());
		
		for (int i = 0; i <ListFilmForMedia.lstTap.size() ; i++) {
			Log.d("lst Tap "+(i+1)+" ::::::", ListFilmForMedia.lstTap.get(i)+"//"+ListFilmForMedia.lstFilmUrl.get(i));
		}
		
		int tap=0;
		
		//Neu so tap lon hon 1
		if (ListFilmForMedia.lstTap.size()>1) {
			gDanhSachTapPhim.setAdapter(dspAdapter);
			//Key sẽ bằng key 1
			
			
		}
		
		//Load actor su dung lazyload
				TableRow tr = new TableRow(ctx);
				Log.d("ActorData.lstActorUrl.size()::::", ""+ActorData.lstActorUrl.size());
				for (int i = 0; i < ActorData.lstActorUrl.size(); i++) {
					idx=i;
					LinearLayout ln = new LinearLayout(ctx);
					TableRow.LayoutParams pararams =  new TableRow.LayoutParams(100,android.widget.TableRow.LayoutParams.MATCH_PARENT);
					pararams.setMargins(0, 0, 10, 0);
					ln.setLayoutParams(pararams);
					ln.setOrientation(LinearLayout.VERTICAL);
					ln.setGravity(Gravity.CENTER_VERTICAL);
					
					//Bat su kien click vao actor
					/*ln.setOnClickListener(new OnClickListener() {
						
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Toast.makeText(ctx, ActorData.lstActorID.get(idx), Toast.LENGTH_SHORT).show();
						}
					});*/
					ImageView iv = new ImageView(ctx);
					iv.setImageResource(R.drawable.movienoimg);
					iv.setPadding(0, 0, 5, 0);
					iv.setScaleType(ScaleType.FIT_CENTER);
					iv.setMaxHeight(150);
					iv.getWidth();
					iv.setLayoutParams(new TableRow.LayoutParams(100,150));
					//Bat su kien click vao hinh anh cua actor
					Log.d("idx::::::", ""+idx+"::::::"+ActorData.lstActorID.get(idx));
					final String id = ActorData.lstActorID.get(i);
					iv.setOnClickListener(new OnClickListener() {
						
						public void onClick(View v) {
							// TODO Auto-generated method stub
							ThongTinDienVien.ID = id;
							ctx.startActivity(new Intent(ctx,actActorInfo.class));
						}
					});
					String imgLink = ActorData.lstActorUrl.get(i);
					DownloadImages down = new DownloadImages(ctx, imgLink, iv);
					down.execute();
					TextView tv = new TextView(ctx);
					tv.setTextSize(12);
					tv.setText(ActorData.lstActorName.get(i));
					tv.setGravity(Gravity.CENTER);
					tv.setMaxLines(2);
					ln.addView(iv);
					ln.addView(tv);
					tr.addView(ln);
				}
				tblActors.addView(tr);
	}
	
	public static void dialogShow(Context ctx){
		LoadPhim = new ProgressDialog(ctx);
		LoadPhim.setMessage("Đang tải dữ liệu phim. Vui lòng đợi!!!");
		LoadPhim.setIndeterminate(true);
		LoadPhim.setCancelable(true);
		LoadPhim.show();
	}
	
	
	public static void PlayFilm(){
		Intent intent = new Intent(ctx, Nahi_MediaActivity.class);
		Bundle newBundle = new Bundle();
		newBundle.putString("Link",paramConst.URL_XEM_PHIM+"?u="+userData.Username);
		Log.d("session Bundle link:::	", paramConst.URL_XEM_PHIM+"?u="+userData.Username);
		intent.putExtras(newBundle);
		ctx.startActivity(intent);
		
		
		
	}
	
	
	
}
